package 链表与双指针.m03.d06.t03;

import 链表与双指针.ListNode;

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public static ListNode alwaysEquals(ListNode node) {
        while (node != null && node.next != null) {
            if(node.val == node.next.val) {
                node = node.next;
            } else {
                break;
            }
        }
        return node.next;
    }
    public ListNode deleteDuplicates(ListNode head) {
        if(head == null) {
            return head;
        }
        if(head.next == null) {
            return head;
        }
        while (head != null && head.next != null) {
            if(head.val == head.next.val) {
                head = alwaysEquals(head);
            } else {
                break;
            }
        }
        ListNode cur = head, prev = cur;
        while (cur != null && cur.next != null) {
            if(cur.val != cur.next.val) {
                prev = cur;
                cur = cur.next;
            } else {
                cur = alwaysEquals(cur);
                prev.next = cur;
            }
        }
        return head;
    }
}